package lelouet.datacenter.simulation.events;

import java.util.List;

import lelouet.datacenter.simulation.Event;
import lelouet.datacenter.simulation.EventHandler;
import lelouet.datacenter.simulation.Hypervisor;
import lelouet.datacenter.simulation.VM;

/** A VM has been allocated/unallocated to an hypervisor. */
public class VMMove extends VMEvent {

	public final Hypervisor hypervisor;

	public static enum VMMoves {
		NEWRUNNING, NEWSLEEPING, DEPARTURE, IMMIGRATIONSTARTED, EMMIGRATIONSTARTED
	}

	public final VMMoves move;

	public VMMove(VM vm, long time, Hypervisor hypervisor, VMMoves move) {
		super(vm, time);
		this.hypervisor = hypervisor;
		this.move = move;
	}

	@Override
	public List<Event> apply(EventHandler handler) {
		return handler.handleMove(this);
	}

	private String toString;

	@Override
	public String toString() {
		if (toString == null) {
			String action;
			switch (move) {
			case DEPARTURE:
				action = "has left";
				break;
			case NEWRUNNING:
				action = "put to run on";
				break;
			case NEWSLEEPING:
				action = "put to sleep on";
				break;
			case EMMIGRATIONSTARTED:
				action = "starting emmigration from";
				break;
			case IMMIGRATIONSTARTED:
				action = "starting immigration to";
				break;
			default:
				throw new UnsupportedOperationException();
			}
			toString = "VM <" + vm.getId() + "> " + action + " hypervisor <"
					+ hypervisor.getId() + ">";
		}
		return toString;
	}

}
